package club.haomoon.boss.config;

import club.haomoon.core.config.GlobalConfig;
import club.haomoon.core.util.PropertyUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import jakarta.annotation.Resource;
/**
 * -------------------------
 * @Description:  Web MVC 配置
 * @Author: Wangjianwei
 * @Date: 2025/02/14 16:33
 * -------------------------
 */
@Configuration
public class BossMvcConfig implements WebMvcConfigurer {

    @Resource
    FastCmsBossInterceptor fastCmsBossInterceptor;

    /**
     * 配置允许跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    /**
     * 登录拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(fastCmsBossInterceptor)
                .addPathPatterns("/**");
    }

    /**
     * 资源目录映射
     */
    @Override
    public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
        String directory = PropertyUtil.get("cms.upload-directory");
        registry.addResourceHandler("/"+ GlobalConfig.adminPublicPrefix +"/**")
                .addResourceLocations("file:" + directory);
    }

}
